<template>
  <div>
    <!-- inline:行内表单 代表一行可以放置多个表单元素 -->
    <el-form :inline="true" class="demo-form-inline" :model="cForm">
      <el-form-item label="一级分类">
        <!-- 该组件上有一个自定义事件 @change -->
        <el-select
          placeholder="请选择"
          v-model="cForm.category1Id"
          @change="handle1"
          :disabled="show"
        >
          <!-- 接口是通过id获取下一级的数据 所以用v-model获取响应的option里得到的id-->
          <el-option
            :label="c1.name"
            :value="c1.id"
            v-for="(c1, index) in list1"
            :key="c1.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择"
          v-model="cForm.category2Id"
          @change="handle2"
          :disabled="show"
        >
          <el-option
            :label="c2.name"
            :value="c2.id"
            v-for="(c2, index) in list2"
            :key="c2.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          placeholder="请选择"
          v-model="cForm.category3Id"
          @change="handle3"
          :disabled="show"
        >
          <el-option
            :label="c3.name"
            :value="c3.id"
            v-for="(c3, index) in list3"
            :key="c3.id"
          ></el-option>
        </el-select>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "CategorySelect",
  props:["show"],
  data() {
    return {
      list1: [],
      //收集响应的一级二级三级分类的id
      cForm: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
      },
      list2: [],
      list3: [],
    };
  },
  mounted() {
    //获取一级分类数据的方法
    this.getCategory1List();
  },
  methods: {
    async getCategory1List() {
      //获取一级分类的请求
      let result = await this.$API.attr.reqCategory1List();
      if (result.code == 200) {
        this.list1 = result.data;
      }
    },
    //一级分类的select事件回调 当一级分类出现变化时获取相应二级分类
    async handle1() {
      //清除数据
      this.list2 = [];
      this.list3 = [];
      this.cForm.category2Id = "";
      this.cForm.category3Id = "";

      const { category1Id } = this.cForm;
      this.$emit("getCategoryId", {categoryId:category1Id,level:1});

      let result = await this.$API.attr.reqCategory2List(category1Id);
      if (result.code == 200) {
        this.list2 = result.data;
      }
    },
    //二级分类获取三级分类
    async handle2() {
      //清除数据
      this.list3 = [];
      this.cForm.category3Id = "";

      const { category2Id } = this.cForm;
      this.$emit("getCategoryId", {categoryId:category2Id,level:2});

      let result = await this.$API.attr.reqCategory3List(category2Id);
      if (result.code == 200) {
        this.list3 = result.data;
      }
    },

    //三级分类改变的事件
    handle3() {
      const { category3Id } = this.cForm;
      this.$emit("getCategoryId", {categoryId:category3Id,level:3});
    },
  },
};
</script>

<style>
</style>